package com.etlmaster.executor.steps.rowprocessor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.etlmaster.executor.Step;
import com.etlmaster.executor.StepMeta;
import com.etlmaster.executor.annotation.StepInstruction;
import com.etlmaster.executor.utils.DataCollectionUtils;
import com.etlmaster.executor.utils.StepState;
import com.etlmaster.jdbc.JdbcTemplate;

import freemarker.core.FreeMarkerTemplate;

@StepInstruction("rowProcessProcessor")
public class ProcessorStep extends Step{

	private ProcessorStepMeta meta;
	public ProcessorStep(StepMeta meta) {
		super(meta);
		this.meta=(ProcessorStepMeta) meta;
	}

	@Override
	protected int handle() {
		Map<String,String> map=new HashMap<String,String>();
		map.put("type", "processor");
		map.put("id", meta.getId());
		map.put("name", meta.getName());
		map.put("code", meta.getId());
		map.put("class", "com.etlmaster.rowprocessor.core.Processor");
		Map<String,Object> data=new HashMap<String,Object>();
		data.put("tableName", meta.getTableName());
		String sql=meta.variableSubstitution(DataCollectionUtils.getProperties("fetchRuleList"),data);
		JdbcTemplate jdbc=new JdbcTemplate();
		List list=jdbc.queryForList(sql);
		data.put("processors", list);
		String changeMap=FreeMarkerTemplate.getFpls("rowProcess", data);
		map.put("param", changeMap);
		String rowstep=FreeMarkerTemplate.getFpl("plugin", map);
		List<String> rowSteps=(List<String>) meta.getVariable("jobJson");
		rowSteps.add(rowstep);
		meta.getStepState().getReturnValue().put("jobJson", rowSteps);
		meta.log("INFO","生成{}配置成功",meta.getName());
		return StepState.RUNNING_SUCCESSED;
	}

	@Override
	public boolean stop() {
		// TODO Auto-generated method stub
		return false;
	}

}
